<template>
  <div class="shouye">
    <div class="box_top">
      <div class="top">
        <h1>饿了吗</h1>
        <h2>···</h2>
      </div>
      <div class="input">
        <input type="text" placeholder="请输入搜索内容" />
      </div>
    </div>
    <div class="box_banner">
      <div class="banner"></div>
    </div>
    <div class="box_center">
      <h1>猜你喜欢</h1>
    </div>
    <div class="main">
      <div class="nav">
        <p
          v-for="(item, index) in nav"
          :key="index"
          @click="sortord(item, index)"
        >
          {{ item }}
        </p>
      </div>
      <div class="list">
        <div class="shop" v-for="(item, index) in render || list" :key="index">
          <img :src="item.picUrl" alt="" />
          <div class="text">
            <h5>{{ item.name }}</h5>
            <h3>{{ item.distance }}</h3>
            <h4>{{ item.monthSalesTip }}</h4>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { home_list } from "../../ajax/index.js";

import { ref, onMounted } from "vue";

var nav = ref(["综合排序", "距离最近", "销量最高", "筛选"]);
var list = ref();
var dislist = ref();
var countlist = ref();
var indexs = ref();
var render = ref();

onMounted(() => {
  home_list().then((res) => {
    console.log(res.data);
    list.value = res.data.list;
    console.log(list.value);
    dislist.value = [...list.value];
    countlist.value = [...list.value];
    for (let i = 0; i < dislist.value.length; i++) {
      for (let j = i + 1; j < dislist.value.length; j++) {
        if (
          parseFloat(dislist.value[i].distance) >
          parseFloat(dislist.value[j].distance)
        ) {
          var temp;
          temp = dislist.value[i];
          dislist.value[i] = dislist.value[j];
          dislist.value[j] = temp;
        }
      }
    }
    for (let i = 0; i < countlist.value.length; i++) {
      for (let j = i + 1; j < countlist.value.length; j++) {
        if (
          Number(countlist.value[i].monthSalesTip.slice(2)) <
          Number(countlist.value[j].monthSalesTip.slice(2))
        ) {
          var temp;
          temp = countlist.value[i];
          countlist.value[i] = countlist.value[j];
          countlist.value[j] = temp;
        }
      }
    }
  });
});

console.log(dislist.value);

var sortord = (item, index) => {
  if (indexs.value === index) {
    indexs.value = null;
    render.value = [...list.value];
  } else {
      indexs.value = index;
    if (item == "距离最近") {
      render.value = [...dislist.value];
    }else if (item == "销量最高") {
      render.value = [...countlist.value];
    }else{
       render.value = [...list.value]
    }
  }
};
</script>

<style scoped>
.box_top {
  height: 80px;
  box-sizing: border-box;
  padding: 10px;
  background-color: cornflowerblue;
}

.top {
  width: 100%;
  display: flex;
  justify-content: space-between;
  height: 30px;
  color: #fff;
}

.input {
  width: 100%;
  height: 30px;
}

input {
  height: 30px;
  border: 1px solid #eee;
  border-radius: 30px;
  width: 90%;
  margin-left: 2%;
  padding-left: 20px;
}

.box_banner {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  height: 150px;
}

.banner {
  width: 100%;
  background-color: blueviolet;
  border-radius: 5px;
  height: 100%;
}

.box_center {
  height: 30px;
  padding-left: 10px;
}

.box_center h1 {
  line-height: 30px;
  font-size: 20px;
  font-weight: 700;
}

.nav {
  padding: 0 10px;
  height: 30px;
  display: flex;
  justify-content: space-around;
}

p {
  height: 20px;
  width: 21%;
  background-color: #eee;
  border-radius: 5px;
  margin-top: 5px;
  text-align: center;
  color: #000;
}

.list {
  margin-top: 20px;
  height: 310px;
  overflow-y: auto;
}

.shop {
  margin-bottom: 10px;
  height: 120px;
  display: flex;
  box-sizing: border-box;
  padding: 10px;
}

.shop img {
  height: 100px;
  margin-top: 10px;
  width: 100px;
}

.shop .text {
  padding: 20px;
  box-sizing: border-box;
}

h5,
h3,
h4 {
  height: 30px;
  font-size: 12px;
}

.change {
  color: rgb(235, 77, 77);
}
</style>